package com.zenithmind.document.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 文档访问日志VO
 * 遵循单一职责原则：专注于访问日志数据展示
 * 遵循开闭原则：支持字段的扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档访问日志VO")
public class DocumentAccessLogVO {

    @Schema(description = "访问日志ID", example = "log123456")
    private String id;

    @Schema(description = "文档ID", example = "doc123456")
    private String documentId;

    @Schema(description = "文档标题", example = "项目需求文档")
    private String documentTitle;

    @Schema(description = "文档类型", example = "PDF")
    private String documentType;

    @Schema(description = "用户ID", example = "user123456")
    private String userId;

    @Schema(description = "用户名", example = "张三")
    private String userName;

    @Schema(description = "用户昵称", example = "小张")
    private String userNickname;

    @Schema(description = "用户头像", example = "https://example.com/avatar.jpg")
    private String userAvatar;

    @Schema(description = "访问类型", example = "VIEW")
    private String accessType;

    @Schema(description = "访问类型描述", example = "查看")
    private String accessTypeDesc;

    @Schema(description = "用户代理", example = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
    private String userAgent;

    @Schema(description = "IP地址", example = "192.168.1.100")
    private String ipAddress;

    @Schema(description = "访问来源", example = "WEB")
    private String accessSource;

    @Schema(description = "访问来源描述", example = "网页端")
    private String accessSourceDesc;

    @Schema(description = "设备类型", example = "PC")
    private String deviceType;

    @Schema(description = "设备类型描述", example = "个人电脑")
    private String deviceTypeDesc;

    @Schema(description = "浏览器类型", example = "Chrome")
    private String browserType;

    @Schema(description = "浏览器版本", example = "120.0.0.0")
    private String browserVersion;

    @Schema(description = "操作系统", example = "Windows 10")
    private String operatingSystem;

    @Schema(description = "操作系统版本", example = "10.0.19045")
    private String osVersion;

    @Schema(description = "屏幕分辨率", example = "1920x1080")
    private String screenResolution;

    @Schema(description = "访问时长（秒）", example = "120")
    private Long accessDuration;

    @Schema(description = "访问时长描述", example = "2分钟")
    private String accessDurationDesc;

    @Schema(description = "页面停留时间（秒）", example = "60")
    private Long stayDuration;

    @Schema(description = "停留时间描述", example = "1分钟")
    private String stayDurationDesc;

    @Schema(description = "引用页面", example = "https://example.com/documents")
    private String referrerUrl;

    @Schema(description = "访问路径", example = "/document/view/123")
    private String accessPath;

    @Schema(description = "查询参数", example = "?tab=content&page=1")
    private String queryParams;

    @Schema(description = "会话ID", example = "session123456")
    private String sessionId;

    @Schema(description = "地理位置", example = "北京市朝阳区")
    private String location;

    @Schema(description = "国家", example = "中国")
    private String country;

    @Schema(description = "省份", example = "北京市")
    private String province;

    @Schema(description = "城市", example = "北京市")
    private String city;

    @Schema(description = "经度", example = "116.4074")
    private Double longitude;

    @Schema(description = "纬度", example = "39.9042")
    private Double latitude;

    @Schema(description = "网络类型", example = "WiFi")
    private String networkType;

    @Schema(description = "网络类型描述", example = "无线网络")
    private String networkTypeDesc;

    @Schema(description = "访问状态", example = "SUCCESS")
    private String accessStatus;

    @Schema(description = "访问状态描述", example = "成功")
    private String accessStatusDesc;

    @Schema(description = "错误代码", example = "404")
    private String errorCode;

    @Schema(description = "错误信息", example = "文档不存在")
    private String errorMessage;

    @Schema(description = "响应时间（毫秒）", example = "150")
    private Long responseTime;

    @Schema(description = "响应时间描述", example = "150毫秒")
    private String responseTimeDesc;

    @Schema(description = "数据传输量（字节）", example = "1024000")
    private Long dataTransferred;

    @Schema(description = "数据传输量描述", example = "1MB")
    private String dataTransferredDesc;

    @Schema(description = "访问时间", example = "2025-01-09T10:30:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime accessTime;

    @Schema(description = "访问时间描述", example = "2025年1月9日 10:30")
    private String accessTimeDesc;

    @Schema(description = "扩展属性", example = "{\"feature\": \"preview\", \"version\": \"1.0\"}")
    private String extendedProperties;

    @Schema(description = "备注", example = "用户通过搜索访问")
    private String remarks;

    @Schema(description = "是否异常访问", example = "false")
    private Boolean isAbnormal;

    @Schema(description = "是否机器人访问", example = "false")
    private Boolean isBot;

    @Schema(description = "是否移动设备", example = "false")
    private Boolean isMobile;

    @Schema(description = "是否新用户", example = "true")
    private Boolean isNewUser;

    @Schema(description = "访问评分", example = "85")
    private Integer accessScore;

    @Schema(description = "访问质量", example = "GOOD", allowableValues = {"EXCELLENT", "GOOD", "NORMAL", "POOR"})
    private String accessQuality;

    @Schema(description = "访问标签", example = "高频用户,正常访问")
    private String accessTags;

    @Schema(description = "风险等级", example = "LOW", allowableValues = {"LOW", "MEDIUM", "HIGH", "CRITICAL"})
    private String riskLevel;

    @Schema(description = "创建时间", example = "2025-01-09T10:30:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间", example = "2025-01-09T10:30:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取访问类型描述
     */
    public String getAccessTypeDescription() {
        if (accessType == null) return "";
        
        switch (accessType.toUpperCase()) {
            case "VIEW": return "查看";
            case "DOWNLOAD": return "下载";
            case "SHARE": return "分享";
            case "EDIT": return "编辑";
            case "DELETE": return "删除";
            case "UPLOAD": return "上传";
            case "COPY": return "复制";
            case "MOVE": return "移动";
            case "RENAME": return "重命名";
            case "COMMENT": return "评论";
            default: return accessType;
        }
    }

    /**
     * 获取访问来源描述
     */
    public String getAccessSourceDescription() {
        if (accessSource == null) return "";
        
        switch (accessSource.toUpperCase()) {
            case "WEB": return "网页端";
            case "MOBILE": return "移动端";
            case "API": return "API接口";
            case "DESKTOP": return "桌面端";
            case "MINI_PROGRAM": return "小程序";
            case "H5": return "H5页面";
            default: return accessSource;
        }
    }

    /**
     * 获取设备类型描述
     */
    public String getDeviceTypeDescription() {
        if (deviceType == null) return "";
        
        switch (deviceType.toUpperCase()) {
            case "PC": return "个人电脑";
            case "MOBILE": return "手机";
            case "TABLET": return "平板";
            case "TV": return "智能电视";
            case "WATCH": return "智能手表";
            case "OTHER": return "其他设备";
            default: return deviceType;
        }
    }

    /**
     * 获取访问状态描述
     */
    public String getAccessStatusDescription() {
        if (accessStatus == null) return "";
        
        switch (accessStatus.toUpperCase()) {
            case "SUCCESS": return "成功";
            case "FAILED": return "失败";
            case "TIMEOUT": return "超时";
            case "UNAUTHORIZED": return "未授权";
            case "FORBIDDEN": return "禁止访问";
            case "NOT_FOUND": return "未找到";
            default: return accessStatus;
        }
    }

    /**
     * 获取时长描述
     */
    public String getDurationDescription(Long duration) {
        if (duration == null || duration <= 0) return "0秒";
        
        long hours = duration / 3600;
        long minutes = (duration % 3600) / 60;
        long seconds = duration % 60;
        
        StringBuilder sb = new StringBuilder();
        if (hours > 0) sb.append(hours).append("小时");
        if (minutes > 0) sb.append(minutes).append("分钟");
        if (seconds > 0 || sb.length() == 0) sb.append(seconds).append("秒");
        
        return sb.toString();
    }

    /**
     * 获取数据大小描述
     */
    public String getDataSizeDescription(Long bytes) {
        if (bytes == null || bytes <= 0) return "0B";
        
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double size = bytes.doubleValue();
        
        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }
        
        return String.format("%.2f%s", size, units[unitIndex]);
    }

    /**
     * 判断是否为成功访问
     */
    public boolean isSuccessfulAccess() {
        return "SUCCESS".equalsIgnoreCase(accessStatus);
    }

    /**
     * 判断是否为长时间访问
     */
    public boolean isLongAccess() {
        return accessDuration != null && accessDuration > 300; // 超过5分钟
    }

    /**
     * 判断是否为高质量访问
     */
    public boolean isHighQualityAccess() {
        return isSuccessfulAccess() && 
               stayDuration != null && stayDuration > 30 && // 停留超过30秒
               responseTime != null && responseTime < 1000; // 响应时间小于1秒
    }
}
